﻿using System;
using System.Net;
using System.Windows;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Ink;
using System.Windows.Input;
using Corina.Datamodel.Entities;
using System.Linq;
using Corina.Enumerations;
using Corina.Commands;

namespace Corina.Cydee.Apphub.Viewmodels
{
    public class SuburlViewmodel : ViewmodelBase
    {
        public SuburlViewmodel()
        {
            WeburlClient.GetAllTopurlCompleted += GetAllTopurlCompleted;
            WeburlClient.GetAllSuburlCompleted += GetAllSuburlCompleted;
            WeburlClient.UpdateSuburlCompleted += UpdateSuburlCompleted;
            WeburlClient.InsertSuburlCompleted += InsertSuburlCompleted;
            WeburlClient.RemoveSuburlCompleted += RemoveSuburlCompleted;
            if (DataContext.IsValid)
            {
                LoadData();
            }
        }

        private void RemoveSuburlCompleted(object sender, DataService.RemoveSuburlCompletedEventArgs e)
        {
            LoadData();
        }

        private void InsertSuburlCompleted(object sender, DataService.InsertSuburlCompletedEventArgs e)
        {
            Operation = EntityOperation.None;
            UpdateEntity = new WeburlInfo();
            LoadData();
        }

        private void UpdateSuburlCompleted(object sender, DataService.UpdateSuburlCompletedEventArgs e)
        {
            Operation = EntityOperation.None;
            UpdateEntity = new WeburlInfo();
            LoadData();
        }

        private void GetAllSuburlCompleted(object sender, DataService.GetAllSuburlCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                if (e.Result.Result == ResultValue.Succeed)
                {
                    SuburlCollection = new ObservableCollection<WeburlInfo>(e.Result.Entities.OfType<WeburlInfo>());
                }
            }
            IsBusy = false;
        }

        private void GetAllTopurlCompleted(object sender, DataService.GetAllTopurlCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                if (e.Result.Result == ResultValue.Succeed)
                {
                    TopurlCollection = new ObservableCollection<WeburlTopInfo>(e.Result.Entities.OfType<WeburlTopInfo>());
                    WeburlClient.GetAllSuburlAsync(Account, Password, "", SelectedPlatform);
                }
            }
        }

        protected override void LoadData(params object[] paras)
        {
            IsBusy = true;
            WeburlClient.GetAllTopurlAsync(Account, Password, "", SelectedPlatform);
        }

        
        #region Properties
        
        private WeburlInfo _updateEntity = new WeburlInfo();
        public WeburlInfo UpdateEntity
        {
            get { return _updateEntity; }
            set
            {
                _updateEntity = value;
                RaisePropertyChanged(() => UpdateEntity);
            }
        }

        private PlatformTypeValue _selectedPlatform = PlatformTypeValue.Manager;
        public PlatformTypeValue SelectedPlatform
        {
            get { return _selectedPlatform; }
            set
            {
                _selectedPlatform = value;
                RaisePropertyChanged(() => SelectedPlatform);
                LoadData();
            }
        }

        #endregion

        #region Topurl
        private ObservableCollection<WeburlTopInfo> _topurlCollection;
        public ObservableCollection<WeburlTopInfo> TopurlCollection
        {
            get { return _topurlCollection; }
            set
            {
                _topurlCollection = value;
                RaisePropertyChanged(() => TopurlCollection);
            }
        }

        private WeburlTopInfo _selectedTopurl;
        public WeburlTopInfo SelectedTopurl
        {
            get { return _selectedTopurl; }
            set
            {
                _selectedTopurl = value;
                RaisePropertyChanged(() => SelectedTopurl);
                RaisePropertyChanged(() => DisplaySuburlCollection);
            }
        }

        private WeburlTopInfo _selectedUpdateTopurl;
        public WeburlTopInfo SelectedUpdateTopurl
        {
            get { return _selectedUpdateTopurl; }
            set
            {
                _selectedUpdateTopurl = value;
                RaisePropertyChanged(() => SelectedUpdateTopurl);
                if(SelectedUpdateTopurl!=null)
                {
                    UpdateEntity.TopurlId = SelectedUpdateTopurl.Id;
                }
            }
        }
        #endregion

        #region Suburl
        private ObservableCollection<WeburlInfo> _suburlCollection;
        public ObservableCollection<WeburlInfo> SuburlCollection
        {
            get { return _suburlCollection; }
            set
            {
                _suburlCollection = value;
                RaisePropertyChanged(() => SuburlCollection);
                RaisePropertyChanged(() => DisplaySuburlCollection);
            }
        }

        public IEnumerable<WeburlInfo> DisplaySuburlCollection
        {
            get { return (SelectedTopurl == null || SuburlCollection == null) ? null : SuburlCollection.Where(t => t.TopurlId == SelectedTopurl.Id); }
        }
        #endregion

        #region Command
        public ICommand InsertCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate {
                        UpdateEntity = new WeburlInfo();
                        //ShowUpdate = true;
                        Operation = EntityOperation.Insert;
                    }
                };
            }
        }

        public ICommand UpdateCommand
        {
            get
            {
                return new DelegateCommand(t =>
                {
                    int id = Convert.ToInt32(t);
                    UpdateEntity = SuburlCollection.FirstOrDefault(i => i.Id == id);
                    SelectedUpdateTopurl = TopurlCollection.FirstOrDefault(i => i.Id == UpdateEntity.TopurlId);
                    //ShowUpdate = true;
                    Operation = EntityOperation.Update;
                });
            }
        }

        public ICommand RemoveCommand
        {
            get
            {
                return new DelegateCommand(t =>
                {
                    if (MessageBox.Show("确定要删除该链接吗？", "删除确认", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                    {
                        int id = Convert.ToInt32(t);
                        WeburlClient.RemoveSuburlAsync(Account, Password, id);
                    }
                });
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        Operation = EntityOperation.None;
                        UpdateEntity = new WeburlInfo();
                    }
                };
            }
        }

        public ICommand SubmitCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        if (Operation == EntityOperation.Insert)
                        {
                            WeburlClient.InsertSuburlAsync(Account, Password, UpdateEntity.Title, UpdateEntity.Url, UpdateEntity.PageCode, UpdateEntity.TopurlId, 0, UpdateEntity.Index, UpdateEntity.ShowToAll);
                        }
                        else
                        {
                            WeburlClient.UpdateSuburlAsync(Account, Password, UpdateEntity.Title, UpdateEntity.Url, UpdateEntity.PageCode, UpdateEntity.TopurlId, 0, UpdateEntity.Index, UpdateEntity.ShowToAll, UpdateEntity.Id);
                        }
                    }
                };
            }
        }
        #endregion
    }
}
